package com.spring.demo.core.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.activation.DataSource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.util.ByteArrayDataSource;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.velocity.VelocityEngineUtils;

import com.spring.demo.common.AppConstants;
import com.spring.demo.core.entity.Attachment;
import com.spring.demo.core.entity.Email;
import com.spring.demo.core.entity.Member;
import com.spring.demo.core.service.IMailService;

public class MailService implements IMailService {

	@Autowired
	private JavaMailSenderImpl mailSender;
	@Autowired
	private SimpleMailMessage alertMailMessage;
	@Autowired
	private VelocityEngine velocityEngine;

	@Override
	public void sendMail(String to, String subject, String message) {
		SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
		simpleMailMessage.setFrom(mailSender.getUsername());
		simpleMailMessage.setTo(to);
		simpleMailMessage.setSubject(subject);
		simpleMailMessage.setText(message);
		mailSender.send(simpleMailMessage);
	}

	@Override
	public void sendAlertMail(String alert) {
		SimpleMailMessage simpleMailMessage = new SimpleMailMessage(
				alertMailMessage);
		simpleMailMessage.setText(alert);
		mailSender.send(simpleMailMessage);
	}

	@Override
	public void sendMail(Email email) throws MessagingException {
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		boolean hasAttachment = (email.getAttachments() != null && !email
				.getAttachments().isEmpty());
		MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,
				hasAttachment, "UTF-8");
		messageHelper.setFrom(email.getFrom() == null ? mailSender
				.getUsername() : email.getFrom());
		messageHelper.setTo(email.getTo());
		if (email.getCc() != null) {
			messageHelper.setCc(email.getCc());
		}
		if (email.getBcc() != null) {
			messageHelper.setBcc(email.getBcc());
		}
		messageHelper.setSubject(email.getSubject());
		messageHelper.setText(email.getText(),
				"text/html".equals(email.getMimeType()));

		if (hasAttachment) {
			for (Attachment attachment : email.getAttachments()) {
				String filename = attachment.getFilename();
				DataSource dataSource = new ByteArrayDataSource(
						attachment.getData(), attachment.getMimeType());
				if (attachment.isInline()) {
					messageHelper.addInline(filename, dataSource);
				} else {
					messageHelper.addAttachment(filename, dataSource);
				}
			}
		}
		mailSender.send(mimeMessage);
	}

	@Override
	public void sendEmailWithTemplate(Member member) {

		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, "UTF-8");
		try {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("member", member);
			String text = VelocityEngineUtils.mergeTemplateIntoString(
					velocityEngine, AppConstants.EMAIL_VM_BIRTHDAY, "UTF-8",
					model);

			helper.setFrom(mailSender.getUsername());
			helper.setTo(member.getEmail());
			helper.setSubject("生日快乐！");
			helper.setText(text, true);

			mailSender.send(message);
		} catch (MessagingException e) {
			throw new RuntimeException(e);
		}
	}
}
